// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormatLineBulbItem.cs" company="StyleCop for ReSharper Development Team">
//   Copyright (c) StyleCop for ReSharper Development Team. All rights reserved.
// </copyright>
// <summary>
//   Defines the FormatTextRangeBulbItem type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace StyleCop.For.ReSharper.BulbItems.Readability
{
    #region Using Directives

    using CodeCleanup.Rules;

    using JetBrains.ProjectModel;
    using JetBrains.ReSharper.Psi.CSharp.Tree;
    using JetBrains.TextControl;

    using StyleCop.For.ReSharper.BulbItems.Framework;

    #endregion

    /// <summary>
    /// QuickFix action which formats multiple statements on a single line.
    /// </summary>
    public class FormatLineBulbItem : TransactionalQuickFixBulbItem
    {
        #region Public Methods

        /// <summary>
        /// Performs the QuickFix, formats multiple statements on a single line. We actually format from the beginning of the line before to the end of the line after.
        /// We need to do this because sometimes just formatting the line that you are on will not insert curly brackets.
        /// </summary>
        /// <param name="solution">
        /// Current Solution.
        /// </param>
        /// <param name="textControl">
        /// Current Text Control to modify.
        /// </param>
        public override void ExecuteInner(ISolution solution, ITextControl textControl)
        {
            var line = Utils.GetLineNumberForTextControl(textControl);
            var element = Utils.GetElementAtCaret(solution, textControl);
            var containingElement = element.GetContainingElement<IUsingDirectiveNode>(true);
            
            Utils.FormatLines(solution, textControl.Document, line - 1, line + 1);

            if (containingElement != null)
            {
                new SpacingRules().EqualsMustBeSpacedCorrectly(containingElement.ToTreeNode());
            }
        }

        #endregion
    }
}